package com.aaa.controller;

import com.aaa.pojos.Emp;
import com.aaa.pojos.EmpExpr;
import com.aaa.pojos.PackBean;
import com.aaa.pojos.Result;
import com.aaa.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Resource
    private EmpService empService;

    @GetMapping
    public Result QueryID(String name, Integer gender,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                          @RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "10") int pageSize
    ){
        PackBean<Emp> empPackBean = empService.pageQuery(name,gender,begin,end,page,pageSize);
        return Result.success(empPackBean);
    }

    @PostMapping
    public Result addEmp(@RequestBody Emp emp) throws Exception {
        log.info("添加员工信息{}"+emp);
        empService.addEmp(emp);
        return Result.success();
    }

    @GetMapping({"/list"})
    public Result jobName(){
       List<Emp> list= empService.jobName();
        return Result.success(list);
    }

    @DeleteMapping
    public Result deleteEmp(Integer[] ids){
        log.info("删除员工信息{}"+ Arrays.toString(ids));
        empService.deleteEmp(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id){
       Emp empId = empService.queryId(id);
        return Result.success(empId);
    }

    @PutMapping()
    public Result updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.success();
    }
}
